//
//  PywCommonPywSDKIndiePayApi.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2017/8/21.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonPywSDKIndiePayApi.h"
#import <UIKit/UIKit.h>
#import "RuntimeInvoker.h"
#import "PywCommonSDKParameter.h"
#import "PywCommonMBProgressHUD+PywCommon.h"
#import "PywSdk.h"

@implementation PywCommonPywSDKIndiePayApi


+ (void)initialize
{
    if (self == [PywCommonPywSDKIndiePayApi class])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(payed:)
                                                     name:@"k_pyw_cp_pay_notification"     // 固定支付完成
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(payed:)
                                                     name:@"k_pyw_cp_anypay_notification"  // 任意支付完成
                                                   object:nil];
    }
}

// 支付完成
+ (void)payed:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    int result = [[dict objectForKey:@"result"] intValue];
    NSString *info = [dict objectForKey:@"info"];
    
    if (result == 0) {
        COMLog(@"%@！",info);
        // 发送回调
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:[NSString stringWithFormat:@"%d",CommonSDK_PayState_Finish] forKey:@"result"];
        [dict setValue:@"支付完成！" forKey:@"info"];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCP_XpCommon_PayNotification
                                                            object:nil
                                                          userInfo:dict];
    }
}


/**
 *
 *  @param payInfo 购买产品信息
 */
+ (void)channelApi_StartPay:(NSDictionary *)payInfo
{
    // 当商品信息为空时，跳出
    if (payInfo.count == 0) return;
    
    // 判断字典内，是否包含channel_params字段key
    if ([[payInfo allKeys] containsObject:@"channel_params"]) {
        // 渠道支付
        [[PywSdk manger] pywChannelPay:payInfo];
    }
}

@end
